package com.ninetaildemonfox.zdl.socialproject.mvp.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.TextView;

import com.ninetaildemonfox.zdl.socialproject.base.BaseActivity;
import com.ninetaildemonfox.zdl.socialproject.R;
import com.ninetaildemonfox.zdl.socialproject.mvp.contract.SystemWebContract;
import com.ninetaildemonfox.zdl.socialproject.mvp.presenter.SystemWebPresenter;

import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author NineTailDemonFox
 * @date 2019/7/23 11:38
 * 功能描述： 问题详情界面
 * 联系方式：1037438704@qq.com
 */


public class DetailsWebViewActivity extends BaseActivity implements SystemWebContract {

    @BindView(R.id.image_left_finish)
    ImageView imageleftFinish;
    @BindView(R.id.text_center)
    TextView textCenter;
    @BindView(R.id.text_right)
    TextView textRight;
    private int count = 0;
    @BindView(R.id.webView)
    WebView webView;
    private String mContext, title;
    private int sysid;
    private SystemWebPresenter systemWebPresenter;

    @Override
    protected int getLayoutResource() {
        return R.layout.activity_details_web_view;
    }

    @Override
    protected void onInitialization(Bundle bundle) {
        Bundle extras = this.getIntent().getExtras();
        if (extras.getInt("count") + "" != null) {
            count = extras.getInt("count");
        }
        if (extras.getInt("sysid") + "" != null) {
            sysid = extras.getInt("sysid");
        }

        systemWebPresenter = new SystemWebPresenter(this, me);
        mContext = extras.getString("mContext");
        title = extras.getString("title");
        if (count == 1) {
            textCenter.setText("活动规则");
            initView(mContext);
        } else if (count == 2) {
            textCenter.setText("协议及声明");
            initView(mContext);
        } else if (count == 3) {
            if (sysid == 1) {
                textCenter.setText("系统消息");
            } else if (sysid == 2) {
                textCenter.setText("广播消息");
            } else if (sysid == 3) {
                textCenter.setText("评价消息");
            } else if (sysid == 4) {
                textCenter.setText("缴费提醒");
            }
            // 3系统详情
            systemWebPresenter.getMsgInfo(token, mContext);
            systemWebPresenter.getReadMsg(token, sysid + "", mContext);
        } else if (count == 4) {
            textCenter.setText("用户协议");
            initView(mContext);
        }else if (count == 5){
            initView(mContext);
        }

    }

    private void initView(String count) {
        imageleftFinish.setVisibility(View.VISIBLE);
        WebSettings settings = webView.getSettings();
        // 加快HTML网页加载完成速度
        if (Build.VERSION.SDK_INT >= 19) {
            settings.setLoadsImagesAutomatically(true);
        } else {
            settings.setLoadsImagesAutomatically(false);
        }
        webView.setWebViewClient(new WebViewClient());
        webView.loadDataWithBaseURL(null, count, "text/html", "utf-8", null);
    }

    @OnClick({R.id.image_left_finish})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.image_left_finish:
                finish();
                break;
            default:
        }
    }

    public static void start(Context me, int count) {
//        1.活动规则 （邀请好友界面进入） 2 协议及声明  3 详情  4 用户协议
        Intent intent = new Intent(me, DetailsWebViewActivity.class);
        Bundle bundle = new Bundle();
        bundle.putInt("count", count);
        intent.putExtras(bundle);
        me.startActivity(intent);
    }

    public static void start(Context me, int count, String mContext) {
//        1.活动规则 （邀请好友界面进入） 2 协议及声明  3 详情  4 用户协议
        Intent intent = new Intent(me, DetailsWebViewActivity.class);
        Bundle bundle = new Bundle();
        bundle.putInt("count", count);
        bundle.putString("mContext", mContext);
        intent.putExtras(bundle);
        me.startActivity(intent);
    }

    public static void start(Context me, int count, String title, String mContext) {
//        1.活动规则 （邀请好友界面进入） 2 协议及声明  3 详情  4 用户协议
        Intent intent = new Intent(me, DetailsWebViewActivity.class);
        Bundle bundle = new Bundle();
        bundle.putInt("count", count);
        bundle.putString("title", title);
        bundle.putString("mContext", mContext);
        intent.putExtras(bundle);
        me.startActivity(intent);
    }

    public static void start(Context me, int sysid, int count, String mContext) {
//        1.活动规则 （邀请好友界面进入） 2 协议及声明  3 详情  4 用户协议
        Intent intent = new Intent(me, DetailsWebViewActivity.class);
        Bundle bundle = new Bundle();
        bundle.putInt("sysid", sysid);
        bundle.putInt("count", count);
        bundle.putString("mContext", mContext);
        intent.putExtras(bundle);
        me.startActivity(intent);
    }


    @Override
    public void Success(Map<String, String> data) {
        initView(data.get("msg_content"));
    }

    @Override
    public void Failed(String msg) {

    }

    @Override
    public void showLoading() {

    }
}
